﻿using Gj;

public class DragListSize : NodeSize
{
    public DragListHelper dragList;
    public float maxWidthRatio;
    public float maxWidth;
    public float offsetWidth;
    public float maxHeightRation;
    public float maxHeight;
    public float offsetHeight;

    protected override void OnRefresh(float rw, float rh, float w, float h, float s)
    {
        SetValue(w / h, w, h, s);
    }

    private void SetValue(float ratio, float width, float height, float scale)
    {
        float _width = 0;
        float _height = 0;
        if (maxWidthRatio > 0)
        {
            if (ratio > maxWidthRatio)
            {
                if (maxWidth > 0)
                    _width = maxWidth;
                else
                    _width = width / ratio * maxWidthRatio / scale;
            }
            else
            {
                _width = width / scale;
            }
        }
        else if (maxWidth > 0)
        {
            var cw = width / scale;
            _width = cw > maxWidth ? maxWidth : cw;
        }

        if (maxHeightRation > 0)
        {
            if (ratio > maxHeightRation)
            {
                _height = height / scale;
            }
            else
            {
                if (maxHeight > 0)
                    _height = maxHeight;
                else
                    _height = height / ratio * maxHeightRation / scale;
            }
        }
        else if (maxHeight > 0)
        {
            var ch = height / scale;
            _height = ch > maxHeight ? maxHeight : ch;
        }

        dragList.RefreshWidthHeight(_width, _height);
    }
}